package com.hospital.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.domain.DTO.ScheduleDTO;
import com.hospital.domain.pojo.Schedule;
import com.hospital.domain.utils.Result;
import com.hospital.mapper.ScheduleMapper;
import com.hospital.service.ScheduleService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule> implements ScheduleService {
    @Override
    public Result setSchedule(Integer dId, Integer depId, String date, String time) {
        if(dId == null || depId == null || date == null || time == null){
            return Result.fail("排班失败");
        }
        LocalDate localDate = LocalDate.parse(date, DateTimeFormatter.ISO_LOCAL_DATE);
        save(new Schedule(dId, depId, localDate, time, 10, 0));
        return Result.ok("排班成功");
    }

    @Override
    public Result queryAllSchedules() {
        List<Schedule> schedules = list();
        return Result.ok(schedules);
    }

    @Override
    public Result querySchedulesByDoctor(Integer dId) {
        if(dId == null){
            return Result.fail("医生ID不能为空");
        }
        List<Schedule> schedules = lambdaQuery().eq(Schedule::getDId, dId).list();
        return Result.ok(schedules);
    }

    @Override
    public Result updateSchedule(ScheduleDTO scheduleDTO) {
        if(scheduleDTO.getDId() == null || scheduleDTO.getDepId() == null || 
           scheduleDTO.getDate() == null || scheduleDTO.getTime() == null){
            return Result.fail("排班信息不完整");
        }
        
        Schedule schedule = lambdaQuery().eq(Schedule::getSId, scheduleDTO.getSId()).one();
        if(schedule == null){
            return Result.fail("排班记录不存在");
        }
        
        LocalDate localDate = LocalDate.parse(scheduleDTO.getDate(), DateTimeFormatter.ISO_LOCAL_DATE);
        schedule.setDId(scheduleDTO.getDId());
        schedule.setDepId(scheduleDTO.getDepId());
        schedule.setDate(localDate);
        schedule.setTime(scheduleDTO.getTime());
        
        updateById(schedule);
        return Result.ok("排班更新成功");
    }

    @Override
    public Result deleteSchedule(Integer sId) {
        if(sId == null){
            return Result.fail("排班ID不能为空");
        }
        
        Schedule schedule = lambdaQuery().eq(Schedule::getSId, sId).one();
        if(schedule == null){
            return Result.fail("排班记录不存在");
        }
        
        removeById(sId);
        return Result.ok("排班删除成功");
    }
    
    @Override
    public Result queryAllSchedulesWithDoctorInfo() {
        List<Map<String, Object>> schedules = baseMapper.queryAllSchedulesWithDoctorInfo();
        return Result.ok(schedules);
    }
    
    @Override
    public Result querySchedulesByDoctorWithInfo(Integer dId) {
        if(dId == null){
            return Result.fail("医生ID不能为空");
        }
        List<Map<String, Object>> schedules = baseMapper.querySchedulesByDoctorWithInfo(dId);
        return Result.ok(schedules);
    }
}
